<%@ page language="java"  pageEncoding="UTF-8"  import="com.knife.news.model.Constants,com.knife.news.object.User"%>
<%@ include file="checkUser.jsp" %>
<%
String tree_id="";
if(request.getParameter("tree_id")!=null){
	tree_id=request.getParameter("tree_id");
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>树分类</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<link rel="stylesheet" href="/include/manage/zTreeStyle/zTreeStyle.css" type="text/css"/>
		<script type="text/javascript" src="/include/manage/js/jquery.js"></script>
		<script type="text/javascript" src="/include/manage/js/jquery-ztree.js"></script>
<SCRIPT LANGUAGE="JavaScript">
<!--
$.ajaxSetup ({
	cache: false
}); 

function clone(jsonObj, newName) {
    var buf;
    if (jsonObj instanceof Array) {
        buf = [];
        var i = jsonObj.length;
        while (i--) {
            buf[i] = clone(jsonObj[i], newName);
        }
        return buf;
    }else if (typeof jsonObj == "function"){
        return jsonObj;
    }else if (jsonObj instanceof Object){
        buf = {};
        for (var k in jsonObj) {
	        if (k!="parentNode") {
	            buf[k] = clone(jsonObj[k], newName);
	            if (newName && k=="name") buf[k] += newName;
	        }
        }
        return buf;
    }else{
        return jsonObj;
    }
}
  
	var zTree1;
	var setting;

	setting = {
		editable: true,
		checkable : false,
		async: true,
		asyncUrl: "getTreeTypeTree.jsp?tree_id=<%=tree_id%>",
		asyncParam: ["name","id"],
		callback: {
			rename: zTreeOnRename,
			beforeRemove: zTreeBeforeDel,
			remove: zTreeOnRemove,
			click: zTreeOnClick,
			drag: zTreeOnDrag,
			drop: zTreeOnDrop,
			rightClick: zTreeOnRightClick
		}
	};

	var zNodes =[];

	$(document).ready(function(){
		refreshTree();

		$("body").bind("mousedown", 
			function(event){
				if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {
					$("#rMenu").hide();
				}
			});
	});

	function showRMenu(type, x, y) {
		$("#rMenu ul").show();
		if (type=="root") {
			$("#m_del").hide();
			$("#m_check").hide();
			$("#m_unCheck").hide();
		}
		$("#rMenu").css({"top":y+"px", "left":x+"px", "display":"block"});
	}
	function hideRMenu() {
		$("#rMenu").hide();
	}

	var rightTargetId="0";
	function zTreeOnRightClick(event, treeId, treeNode) {
		if (!treeNode) {
			zTree1.cancelSelectedNode();
			rightTargetId="0";
			showRMenu("root", event.clientX, event.clientY);
		} else if (treeNode && !treeNode.noR) {
			rightTargetId=treeNode.id;
			if (treeNode.newrole && event.target.tagName != "a" && $(event.target).parents("a").length == 0) {
				zTree1.cancelSelectedNode();
				showRMenu("root", event.clientX, event.clientY);
			} else {
				zTree1.selectNode(treeNode);
				showRMenu("node", event.clientX, event.clientY);
			}
		}
	}
	
	function expandAll(expandSign) {
		zTree1.expandAll(expandSign);
	}

	var addCount = 1;
	function addTreeNode() {
		var newtypename="新增分类" + (addCount++);
		var tempName=window.prompt('请输入分类名称',newtypename);
		if(tempName==""||tempName==null){
		}else{
			var node = zTree1.getSelectedNode();
			//ajax调用服务器端参数，产生新的ID值
			$.get("/manage_type_ajax.ad", { parameter: "add",name: tempName, parent: rightTargetId,tree_id: "<%=tree_id%>",site:"0" }, function(data){
				zTree1.addNodes(node, [{ id:data,name:tempName}]);
			});
		}
		hideRMenu();
	}
	function removeTreeNode() {
		var node = zTree1.getSelectedNode();
		if (node) {
			if (node.nodes && node.nodes.length > 0) {
				var msg = "该分类包含子分类，如果删除将连同子分类一起删掉。\n确认删除吗？";
				if (confirm(msg)==true){
					$.get("/manage_type_ajax.ad", {parameter:"delete", id: node.id } );
					zTree1.removeNode(node);
				}
			} else {
				var msg = "确认删除吗？";
				if (confirm(msg)==true){
					$.get("/manage_type_ajax.ad", {parameter:"delete", id: node.id } );
					zTree1.removeNode(node);
				}
			}
		}
		hideRMenu();
	}
	
	function editTreeNode(){
		var node = zTree1.getSelectedNode();
		if (node) {
			
		}
		hideRMenu();
	}

	function refreshTree() {
		hideRMenu();
		zTree1 = $("#treeList").zTree(setting);
	}
	
	function zTreeOnClick(event, treeId, treeNode){
		if(treeNode.id!="0"){
			window.parent.frames["config"].location.href="/manage_type.ad?parameter=edit&id=" + treeNode.id;
		}
	}
	
	function zTreeOnRename(event, treeId, treeNode) {
		//alert(treeNode.tId + ", " + treeNode.name);
		$.get("/manage_type_ajax.ad", {parameter:"rename", id:treeNode.id, name:treeNode.name});
	}
	
	function zTreeOnRemove(event, treeId, treeNode) {
		//var msg = "确认删除吗？";
		//if (confirm(msg)==true){
			$.get("/manage_type_ajax.ad", {parameter:"delete", id:treeNode.id });
		//}else{
		//	return false;
		//}
	}
	
	var pnodeid,dragNodeIndex;
	function zTreeOnDrag(event, treeId, treeNode) {
		if(treeNode.parentNode!=null){
			pnodeid=treeNode.parentNode.id;
		}else{
			pnodeid="0";	
		}
		dragNodeIndex=zTree1.getNodeIndex(treeNode);
	}
	
	function zTreeOnDrop(event, treeId, treeNode, targetNode, moveType) {
		var pid;
		var order=zTree1.getNodeIndex(treeNode)+1;
		if(moveType=="inner"){//移到之内
			//nodes=zTree1.getNodesByParam("level", targetNode.level+1, targetNode);
			pid=targetNode.id;
		}else if(moveType=="before" || moveType=="after"){//移到之前
			if(targetNode.parentNode!=null){
				//nodes = zTree1.getNodesByParam("level", targetNode.level, targetNode.parentNode);
				pid=targetNode.parentNode.id;
			}else{
				//nodes = zTree1.getNodesByParam("level", targetNode.level, null);
				pid="0";
			}
		}
		changeType(treeNode.id,pid,order);
	}
	
	function changeType(nid,pid,order){
		//alert(nid+"被移动到了栏目:"+pid+"下，顺序是:"+order);
		$.get("/manage_type_ajax.ad", {parameter:"order", id: nid, parent: pid,treeid:"<%=tree_id%>", order: order });
	}
	
	function zTreeBeforeDel(treeId, treeNode) {
		if(confirm('确定要删除吗?')){
		}else{
			return false;
		}
	}
  //-->
</SCRIPT>
<style type="text/css">
body {
 padding: 0px;
 margin: 0px;
 font-size:12px;
}
#treeTitle{
	font-weight:bold;
	background:url('/include/manage/images/main/tab_05.gif');
	/*width:165px;*/
	height:28px;
	margin:3px;
	margin-bottom:0;
	border-left:1px solid #b5d6e6;
	border-right:1px solid #b5d6e6;
	text-indent:15px;
}
#treeTitle a{line-height:28px;color:black;text-decoration:none}
#treeList {
	margin:3px;
	margin-top:0;
	/*width:165px;*/
	height:482px;
	border:1px solid #b5d6e6;
	border-top:0;
	overflow:hidden;
	overflow-y:auto;
}
.tree{padding:0;}
/* ------------- 右键菜单 -----------------  */

div#rMenu {
	background-color:#F9F9F9;
	text-align: left;
	width:72px;
	border-top:1px solid #F1F1F1;
	border-left:1px solid #F1F1F1;
	border-right:1px solid gray;
	border-bottom:1px solid gray;
}

div#rMenu ul {
	list-style: none outside none;
	margin:0px 1px;
	padding:0;
}
div#rMenu ul li {
	cursor: pointer;
	width:70px;
	height:20px;
	line-height:20px;
	text-indent:8px;
	margin:1px 0px;
	font-size:12px;
	border-bottom:1px solid gray;
}
div#rMenu ul li a{display:block;width:100%;text-decoration:none;color:black}
div#rMenu ul li a:hover{
	color:white;
	background-color:darkblue;
}
div#rMenu ul li a:visited{color:black}
</style>
	</head>
<body>
<div id="treeTitle"><img src="/include/manage/zTreeStyle/img/sim/tree.png" align="absmiddle"/>&nbsp;<a href="/manage_type.ad?parameter=list" target="config">所有分类</a></div>
<div id="treeList" class="tree"></div>
<div id="rMenu" style="position:absolute; display:none;">
	<ul id="m_add"><li><a href="javascript:addTreeNode()">新增分类</a></li></ul>
	<ul id="m_del"><li><a href="javascript:removeTreeNode()">删除分类</a></li></ul>
	<ul id="m_check"><li><a href="javascript:refreshTree()">刷新</a></li></ul>
</div>
	</body>
</html>